package com.teckteam.ptvsystem.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.teckteam.ptvsystem.controller.ScrapeFailedException;
import com.teckteam.ptvsystem.model.ResultNode;

/**
 * Journey Planner Results dialog
 *
 */
public class JourneyPlannerResults extends JFrame{
   private JPanel     pnlTitle, // title
                      pnlJourney, //journey details
                      pnlGrid, //journey plan
                      pnlGandH; // journey details + plan
   private JLabel     lblTitle, //title
           
           //Journey details
           lblFrom, lblTo, lblDeparting, lblArriving,
           
           //Journey Plan
           lblTravelBy, lblBlank, lblTime, lblDetails, lblMap, lblInfo, lblCarbon;
   
   
   /**
    * Constructor to display collected results from the Journey Planner
    * @param resultList
    */
   public JourneyPlannerResults(final ArrayList<ResultNode> resultList)
   {
      setLocationByPlatform(true);
      setSize(1152, 700);
      setResizable(true);
      
      GridLayout JourneyLayout = new GridLayout(3,2,225,10);
      GridLayout TableLayout = new GridLayout(0,7);
      Container c = getContentPane();
      Font font = new Font("Verdana", Font.BOLD, 15);
      Font font2 = new Font("Verdana", Font.BOLD, 12);
      JButton viewMap = new JButton("View Map");
      pnlGandH = new JPanel();
      
      pnlTitle = new JPanel();
      lblTitle = new JLabel("Journey Plan Results");
      lblTitle.setFont(new Font("Verdana", Font.BOLD, 24));
      pnlTitle.add(lblTitle);
      
      c.add(pnlTitle, BorderLayout.PAGE_START);

      pnlJourney = new JPanel();
      pnlJourney.setLayout(JourneyLayout);
      
      String fromDetails[] = resultList.get(0).getDestinationDetails();
      lblFrom = new JLabel("From: " + fromDetails[1]);
      lblFrom.setFont(font2);
      pnlJourney.add(lblFrom);
      
      lblDeparting = new JLabel("Departing: " + fromDetails[4]);
      lblDeparting.setFont(font2);
      pnlJourney.add(lblDeparting);
      
      String toDetails[] = resultList.get(resultList.size()-1).getDestinationDetails();
      lblTo = new JLabel("To: " + toDetails[1]);
      lblTo.setFont(font2);
      pnlJourney.add(lblTo);
      
      lblArriving = new JLabel("Arrival: " + toDetails[4]);
      lblArriving.setFont(font2);
      pnlJourney.add(lblArriving);
      
      pnlGandH.add(pnlJourney, BorderLayout.NORTH);
      
      pnlGrid = new JPanel();
      pnlGrid.setLayout(TableLayout);
      pnlGrid.add(lblTravelBy = new JLabel("Hop"));
      lblTravelBy.setBorder(BorderFactory.createLineBorder(Color.BLACK));
      lblTravelBy.setFont(font);
      pnlGrid.add(lblBlank = new JLabel("Station"));
      lblBlank.setBorder(BorderFactory.createLineBorder(Color.BLACK));
      pnlGrid.add(lblTime = new JLabel("Route"));
      lblTime.setBorder(BorderFactory.createLineBorder(Color.BLACK));
      lblTime.setFont(font);
      pnlGrid.add(lblDetails = new JLabel("Suburb"));
      lblDetails.setBorder(BorderFactory.createLineBorder(Color.BLACK));
      lblDetails.setFont(font);
      pnlGrid.add(lblMap = new JLabel("Time"));
      lblMap.setBorder(BorderFactory.createLineBorder(Color.BLACK));
      lblMap.setFont(font);
      pnlGrid.add(lblInfo = new JLabel("Distance(km)"));    
      lblInfo.setBorder(BorderFactory.createLineBorder(Color.BLACK));
      lblInfo.setFont(font);
      pnlGrid.add(lblCarbon = new JLabel("Carbon units ($)"));    
      lblCarbon.setBorder(BorderFactory.createLineBorder(Color.BLACK));
      lblCarbon.setFont(font);
      
      //View google map
      viewMap.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
            try
            {
               new JourneyPlannerMapView(resultList);
            } catch (ScrapeFailedException ex)
            {
               //handled
            }
         }
      });
      
      for(int i = 0; i < resultList.size(); i++){
         /*
            * James if you want the grid to
            * hold JPanels instead of JLabels
            * just change the next line to
            * JPanel row = new JPanel();
            */
         //JLabel row = new JLabel("");
         //JPanel row = new ResultRowPanel(resultList.get(i));
         //row.setBorder(BorderFactory.createLineBorder(Color.BLACK));
         
         //use single row panels with ResultRowPanel instead of cells
         String details[] = resultList.get(i).getDestinationDetails();
         for(int j = 0; j < details.length; j++)
         {
            if(j==4) continue; 
            JPanel cell = new JPanel();
            cell.add(new JLabel(details[j]));
            pnlGrid.add(cell);
         }
      }
      
      pnlGandH.add(pnlGrid, BorderLayout.SOUTH);
      c.add(pnlGandH, BorderLayout.CENTER);
      c.add(viewMap, BorderLayout.SOUTH);
      
      this.setVisible(true);
   }
}